import { CellHeight } from "./GameDefine";
import { EventKey } from "../common/Key";
import { GameData } from "./GameData";

cc.Class({
    extends: cc.Component,

    properties: {
        nodKnotParent: cc.Node,
        spriteBg: cc.Sprite,
        nodCore: cc.Node,
        collider: cc.PolygonCollider,
    },

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, function (touch, event) {
            var touchLoc = touch.getLocation();
            if (!cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
                return;
            }

            G.onFire.fire(EventKey.EVENT_CLICK_CELL, { x: this.gridX, y: this.gridY });
        }, this);
        this._registeEvent();
    },
    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START);
        this._unRegisteEvent();
    },
    _unRegisteEvent() {
        G.onFire.off(EventKey.EVENT_GAME_OVER, this._onFire,this);
    },
    _registeEvent() {
        G.onFire.on(EventKey.EVENT_GAME_OVER, this._onFire, this);

    },
    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_GAME_OVER) {
            this.node.scale = 1.03;
        }
    },

    init(cellModel) {
        this.node.scale = 1;
        let pos = cellModel.getPos();
        if(GameData.succMode){
            pos = cellModel.getSuccPos();
        }
        this.gridX = pos.x;
        this.gridY = pos.y;
        this.isFixed = cellModel.getIsFixed();
        this.knotData = cellModel.getKnotData();
        this.succPos = cellModel.getSuccPos();

        this._setPosition();
        this._addKnots();
        this._setIsFixed();
    },

    moveTo(pos) {
        this.gridX = pos.x;
        this.gridY = pos.y;
        let pixX = this.gridX * CellHeight * Math.cos(30 * Math.PI / 180) - 5 * CellHeight * Math.cos(30 * Math.PI / 180);
        let offsetY = this.gridX % 2 == 0 ? CellHeight / 2 : 0;
        let pixY = offsetY + this.gridY * CellHeight - 4 * CellHeight;

        cc.tween(this.node)
            .to(0.2, { position: cc.v2(pixX, pixY) })
            .start()
    },

    _setPosition() {
        let pixX = this.gridX * CellHeight * Math.cos(30 * Math.PI / 180) - 5 * CellHeight * Math.cos(30 * Math.PI / 180);
        let offsetY = this.gridX % 2 == 0 ? CellHeight / 2 : 0;
        let pixY = offsetY + this.gridY * CellHeight - 4 * CellHeight;
        this.node.position = cc.v2(pixX, pixY);
    },

    _addKnots() {
        for (let i = 0; i < this.knotData.length; i++) {
            let item = this.knotData[i];
            let node = G.utils.getPoolNode('nodePoolKnot');
            node.parent = this.nodKnotParent;
            node.getComponent('ItemKnot').init(item);
        }
    },

    _setIsFixed() {
        let url = 'textures/cellBg1';
        if (this.isFixed == 1) {
            url = 'textures/cellBg2';
        }
        G.common.setSpriteFrame(this.spriteBg, url)
    },

    _setSelectState(select) {
        let url = 'textures/cellBg1';
        if (select) {
            url = 'textures/cellBg3';
        }
        G.common.setSpriteFrame(this.spriteBg, url)
    },

    cancelFixed() {
        let url = 'textures/cellBg1';
        G.common.setSpriteFrame(this.spriteBg, url)
    },

    select() {
        this._setSelectState(true);
    },
    cancelSelect() {
        this._setSelectState(false);
    },
    // update (dt) {},
});
